// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.utils;

import java.util.regex.Pattern;

/**
 * @author lingchuanzhou19320
 * @date 2022/10/18
 */
public class StringUtils {
    /**
     * 合法密码正则表达模版
     */
    private static String[] passwordPatterns = {".*?[A-Z].*?", ".*?[0-9].*?", ".*?[a-z].*?", ".*?[^A-Za-z0-9].*?"};


    /**
     * 检查密码是否合法, 密码需满足大写字母，小写字母，数字和特殊字符任意三种组合，且长度在8到30位之间
     */
    public static Boolean isValidPassword(String string) {
        if(string.length() < 8 || string.length() > 30) {
            return Boolean.FALSE;
        }

        Pattern passwordPatternCls = null;
        int isValidScore = 0;
        for (String passwordPattern: passwordPatterns) {
            passwordPatternCls = Pattern.compile(passwordPattern);
            if(passwordPatternCls.matcher(string).matches()) {
                isValidScore += 1;
            }
        }

        return isValidScore >= 3;
    }
}
